/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package co.edu.unal.behaviours;

import co.edu.unal.agentes.BookDistributorAgent;
import co.edu.unal.agentes.BookSellerAgent;
import jade.core.AID;
import jade.core.behaviours.CyclicBehaviour;
import jade.core.behaviours.TickerBehaviour;
import jade.domain.DFService;
import jade.domain.FIPAAgentManagement.DFAgentDescription;
import jade.domain.FIPAAgentManagement.ServiceDescription;
import jade.domain.FIPAException;
import jade.lang.acl.ACLMessage;
import jade.lang.acl.MessageTemplate;
import java.util.HashMap;
import javax.swing.JTextArea;

/**
 *
 * @author raider
 */
public class PurchaseOrdersServer extends CyclicBehaviour {

    private HashMap catalogue;
    private String agentType;
    JTextArea log;

    public PurchaseOrdersServer(HashMap catalogue, String agentType, JTextArea log) {
        this.catalogue = catalogue;
        this.agentType=agentType;
        this.log = log;
    }
    
    @Override
    public void action() {
        MessageTemplate mt = MessageTemplate.MatchPerformative(ACLMessage.ACCEPT_PROPOSAL);
        ACLMessage msg = myAgent.receive(mt);
        if (msg != null) {
            // ACCEPT_PROPOSAL Message received. Process it
            String[] datos = msg.getContent().split(",");
            ACLMessage reply = msg.createReply();

            Integer price = (Integer) catalogue.remove(datos[0]);
            if (price != null) {
                reply.setPerformative(ACLMessage.INFORM);
//                if (agentType.equals("book-seller")||agentType.equals("book-distributor")) {
//                    reloadStock(datos[0], datos[1],price);
//                }
            } else {
                // The requested book has been sold to another buyer in the meanwhile .
                reply.setPerformative(ACLMessage.FAILURE);
                reply.setContent("not-available");
            }
            myAgent.send(reply);
        } else {
            block();
        }
    }
    
    public void reloadStock(final String bookRequested, final String quantity, final int price){
        AID[] sellerAgents = null;
        DFAgentDescription template = new DFAgentDescription();
        ServiceDescription sd = new ServiceDescription();
        sd.setType(agentType.equals("book-seller") ? "book-distributor" : "book-manufacturer");
        template.addServices(sd);
        try {
            DFAgentDescription[] result = DFService.search(myAgent, template);
            sellerAgents = new AID[result.length];
            for (int i = 0; i < result.length; ++i) {
                sellerAgents[i] = result[i].getName();
            }
        } catch (FIPAException fe) {
            log.setText(log.getText()+ "\n" +"fe.getMessage() = " + fe.getMessage());
            System.out.println("fe.getMessage() = " + fe.getMessage());
        }
        myAgent.addBehaviour(new RequestPerformer(sellerAgents, bookRequested, quantity, log));
    }
}
